<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>项目新增</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../js/formSelects/formSelects-v4.css" media="all">
    <link rel="stylesheet" href="../css/font/iconfont.css" media="all">
    <link rel="stylesheet" href="../js/formSelects/formSelects-v4.css">
    <style>
        .other_name {
            display: inline-block;
            margin-right: 10px;
            cursor: pointer;
        }
        .other_name>i {
            margin-left: 5px;
        }
        .other_name>i:hover {
            color: #FFFFFF;
        }

        .layui-form-label{
            width:140px;
        }
        .layui-input-block{
            margin-left: 170px;
        }
        a{
            color:#fff;
        }
        a:hover {
            color: #00c0ef;
        }
        legend{
            color: #00c0ef
        }
    </style>
</head>
<body>


<div style="margin-top: 40px; margin-right: 30px;">
    <form class="layui-form" action=""  lay-filter="dataForm">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
            <legend>信息编辑</legend>
        </fieldset>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>项目名称</label>
            <div class="layui-input-block">
                <input type="text" name="projectname" id="projectname"  lay-verify="required" placeholder="项目名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>施工单位名称</label>
            <div class="layui-input-block">
                <select name="companyid"  id="companyid" xm-select="companyid" xm-select-radio=""  lay-verify="required"   lay-search="">
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="display: none">
            <label class="layui-form-label"></label>
            <div class="layui-input-block" id="divOtherName">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>项目地址</label>
            <div class="layui-input-block">
                <input type="text" name="projectadress" id="projectadress"  lay-verify="required" placeholder="项目地址" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>项目状态</label>
            <div class="layui-input-block">
                <select name="projectstatus" id="projectstatus" lay-verify="required">
                    <option value=""selected="selected">请选择</option>
                    <option value="在建">在建</option>
                    <option value="竣工">竣工</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>项目类型</label>
            <div class="layui-input-block">
                <select name="projecttype" id="projecttype" lay-verify="required">
                    <option value="" selected="selected">请选择</option>
                    <option value="房建">房建</option>
                    <option value="市政">市政</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>项目负责人</label>
            <div class="layui-input-block">
                <input type="text" name="projectperson" id="projectperson" lay-verify="required" placeholder="项目负责人" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color:red">*</span>项目负责人电话</label>
            <div class="layui-input-block">
                <input type="text" name="projectpersonphone" id="projectpersonphone" lay-verify="required|phone" placeholder="项目负责人电话" class="layui-input">
            </div>
        </div>

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
            <legend>信息提交</legend>
        </fieldset>
        <div class="layui-form-item" id="divSubmit">
            <div class="layui-input-block"  style="text-align: center;">
                <button id="btnSub" class="layui-btn" lay-filter="formSubmit"  lay-submit="">提交</button>
            </div>
        </div>

    </form>
</div>

</body>
<script src="../assets/layui/layui.js"></script>
<script src="../js/common.js"></script>
<script>
    layui.config({
        base: '../js/'
    }).extend({
        formSelects: 'formSelects/formSelects-v4'
    });
    var $;

    layui.use(['jquery','form','validate','common','element', 'laydate','upload','formSelects'], function () {
        var form = layui.form,
                layer = layui.layer,
                validate = layui.validate,
                formSelects = layui.formSelects,
                common = layui.common;
        $ = layui.jquery;

        validate.regVerify();

        var projectId=common.getUrlParam("id");
        var op=common.getUrlParam("op");
        formSelects.config("companyid",{
            keyName: 'name',
            keyVal: 'code'})
            .data("companyid","server",{
                url: '../builder/getdd4Builder?hasAll=1',
                success: function(id, url, searchVal, result){
                    formSelects.value('companyid',result);
                }
            });
        form.render('select');
        formSelects.render();

        $.ajax({
                url: "../project/info",
                dataType: "json",
                data: { projectId: projectId },
                success: function (rtn) {
                    if (rtn && rtn.code===0) {
                        var data = rtn.data;
                        formSelects.value("companyid",data.licno);
                        form.val('dataForm', {
                            projectname: data.projectname
                            , projecttype: data.projecttype
                            , projectadress: data.projectadress
                            , projectstatus: data.projectstatus
                            , companyname: data.companyname
                            , projectperson: data.projectperson
                            , projectpersonphone: data.projectpersonphone
                        });

                        $("#projecttype").find("option").each(function(){
                            if($(this).text()==rtn.data.projecttype){
                                $(this).attr('selected', 'selected');
                            }
                        });
                        // common.initSelectData("#companyname","../company/dd4Company?hasAll=1",function () {
                        //     if(rtn.data.companyname && rtn.data.companyname!=null) {
                        //         $("#companyname").find("option").each(function(){
                        //             if($(this).text()==rtn.data.companyname){
                        //                 $(this).attr('selected', 'selected');
                        //             }
                        //         });
                        //     }
                        //     form.render('select');
                        //     formSelects.render();
                        // });
                        formSelects.value('companyid', rtn.data.licno.split(','));
                        
                        $("#projectstatus").find("option").each(function(){
                            if($(this).text()==rtn.data.projectstatus){
                                $(this).attr('selected', 'selected');
                            }
                        });
                        
                        form.render();
                    }
                }
            });

        form.on('submit(formSubmit)', function (data) {
            var projectname = $("#projectname").val();
            var projecttype = $("#projecttype").find("option:selected").text();
            var projectperson = $("#projectperson").val();
            var projectpersonphone = $("#projectpersonphone").val();
            if(projectname==""||projecttype=="请选择"||projectperson==""||projectpersonphone=="")
            {
                common.showError('必填项不能为空!');
            }
            var projectadress = $("#projectadress").val();
            var projectstatus = $("#projectstatus").find("option:selected").text();
            // var companyname = $("#companyname").find("option:selected").text();
            // var licno = $("#companyname").find("option:selected").val();
            var licno = formSelects.value('companyid', 'valStr');
            var companyname = formSelects.value('companyid', 'nameStr');

            
            data.field.projectname = projectname;
            data.field.projecttype = projecttype;
            data.field.projectperson = projectperson;
            data.field.projectpersonphone = projectpersonphone;
            data.field.projectadress = projectadress;
            data.field.projectstatus = projectstatus;
            data.field.companyname = companyname;
            data.field.projectid = projectId;
            data.field.licno = licno;

            if(projectstatus=="竣工")
            {
                layer.confirm('请注意：报竣工后的项目则不能再进行工资发放的填报！', function (index) {
                    $.ajax({
                        type: 'post',
                        url: '../project/edit',
                        data: {json: JSON.stringify(data.field), op: op},
                        success: function (rtn) {
                            if (rtn.code === 0) {  //数据保存成功
                                var msg = "保存成功!";
                                common.showInfo(msg, function () {
                                    //刷新前页
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.document.getElementById('btnSearch').click();
                                    parent.layer.close(index);
                                });
                            }
                            else {
                                common.showError(rtn.msg);
                            }
                        },
                        error: function (XmlHttpRequest, textStatus, errorThrown) {
                            common.showError('保存错误');
                        }
                    });
                })
            }else {
                $.ajax({
                    type: 'post',
                    url: '../project/edit',
                    data: {json: JSON.stringify(data.field), op: op},
                    success: function (rtn) {
                        if (rtn.code === 0) {  //数据保存成功
                            var msg = "保存成功!";
                            common.showInfo(msg, function () {
                                //刷新前页
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.document.getElementById('btnSearch').click();
                                parent.layer.close(index);
                            });
                        }
                        else {
                            common.showError(rtn.msg);
                        }
                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                        common.showError('保存错误');
                    }
                });
            }
            return false;
        });

    })



</script>
</html>